package com.xiangyang.eos

import android.graphics.Color
import android.graphics.Path
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    var w = 0f
    var h = 0f


    val leftOrRightOffPre = 0.125f
    //底部百分比
    val bottomTrianglePre = 0.2037f
    //顶部高度
    val topLinePre = 0.2593f

    val topTriangPre = 0.3024f

    var leftOrRightOff : Float = 0.0f
    var bottomTriangleOff : Float = 0.0f
    var topLineOff : Float = 0.0f
    var topTraingOff :Float = 0.0f
    var centerX : Float = 0.0f
    val strokeWidth = 4f

    val ZERO = 0.0f
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        w = dpToSpx(250f)
        h = dpToSpx(400f)

        size()

        val path = Path()


        //把画笔移到中间
        path.moveTo(centerX,ZERO)
        //画左边顶部线条
        path.lineTo(leftOrRightOff,topTraingOff)
        //画左边中部线条
        path.lineTo(ZERO,bottomTriangleOff)
        //画左边底部线条
        path.lineTo(centerX,h)
        //画右边底部线条
        path.lineTo(w,bottomTriangleOff)
        //画右边中部线条
        path.lineTo( w - leftOrRightOff,topTraingOff)
        //画右边顶部线条
        path.lineTo(centerX,ZERO)
        //画顶部竖线
        path.lineTo(centerX,topLineOff)
        //画三角形左边线条
        path.lineTo(ZERO,bottomTriangleOff)

        path.lineTo(w,bottomTriangleOff)
//
        path.lineTo(centerX,topLineOff)

        path.moveTo(leftOrRightOff,topTraingOff)

        path.lineTo(centerX,h)

        path.lineTo(w - leftOrRightOff,topTraingOff)
        paths.sourcePath = path
        paths.setColorBg(Color.TRANSPARENT).setColorFg(Color.BLACK);
        paths.setAnimTime(3000).setAnimInfinite(false).startAnim();
        paths.setOnClickListener {
            paths.startAnim()
        }
    }

    fun size()
    {
        topLineOff = h * topLinePre

        leftOrRightOff = w * leftOrRightOffPre

        bottomTriangleOff = h - (h * bottomTrianglePre)

        topTraingOff = h * topTriangPre

        centerX = w / 2 - (strokeWidth / 2)
    }

    fun dpToSpx(dp:Float):Float
    {
       return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, dp,getResources().getDisplayMetrics());
    }

}
